# -*- coding:utf-8 -*-
import logging
from utils.tz import get_utc_date, local_now
from settings import MG as mg
from utils.interface import send_text_msg, send_inline_markup_msg


_LOGGER = logging.getLogger('bot')
NOTIFY_UIDS = [8036187, 8001739, 8003335, 8039163]

LOTTERY_TYPE = {
    1: u"重庆时时彩",
    3: u"江苏快3",
    4: u"山东11选5",
    6: u"天津时时彩",
    7: u"新疆时时彩",
    8: u"江西11选5",
    9: u"广东11选5",
    10: u"上海11选5",
    11: u"广西快3",
    12: u"北京PK10"
}


def notify(msg):
    for uid in NOTIFY_UIDS:
        send_text_msg(1, uid, msg)
        _LOGGER.info(u'notify %s to %s', msg, uid)


def start():
    today_date = local_now()
    day = today_date.strftime('%Y-%m-%d')
    # profit
    daily_report = mg.daily_report.find_one({'_id': day}) or {}
    profit = daily_report.get('real_profit', 0)
    profit_rate = daily_report.get('real_profit_rate', 0)
    if profit_rate < 0:
        msg = u'当前利润异常, 利润值{}, 利润率{}'.format(profit, profit_rate)
        notify(msg)
    # bigbang
    for k, v in LOTTERY_TYPE.items():
        item = mg.activity_stats.find_one({
            '_id': '{}-{}'.format(k, day)
        })
        if not item:
            continue
        pay = item.get('pay', {}).get('total', 0)
        win = item.get('win', {}).get('total', 0)
        bonus = item.get('bonus', {}).get('total', 0)
        refund = item.get('refund', {}).get('total', 0)
        gain = pay - win - bonus - refund
        if pay > 0:
            profit_rate = float(gain) / pay
            if profit_rate < -0.5:
                msg = u'%s数据异常, 利润率%.2f%%\n购买%s,奖金%s,加奖%s,退款%s' % (
                    v, profit_rate*100, pay, win, bonus, refund)
                notify(msg)
    # lottery
    lottery_report = mg.lottery_report.find_one({'_id': day}) or {}
    lottery_profit_rate = lottery_report.get('profit_rate', 0)
    lottery_gain_count = lottery_report.get('gain_count', 0)
    lottery_gain_amount = lottery_report.get('gain_amount', 0)
    lottery_lose_count = lottery_report.get('lose_count', 0)
    lottery_lose_amount = lottery_report.get('lose_amount', 0)
    if lottery_profit_rate < 0:
        msg = u'转转转数据异常, 利润率{}\n赢家人数{},盈利{}\n输家人数{},盈利{}'.format(
            lottery_profit_rate, lottery_gain_count, lottery_gain_amount,
            lottery_lose_count, lottery_lose_amount)
        notify(msg)
    # bull 
    bull_report = mg.bull_report.find_one({'_id': day}) or {}
    bull_profit_rate = bull_report.get('profit_rate', 0)
    bull_sys_bet_amount = bull_report.get('sys_bet_amount', 0)
    bull_sys_award_amount = bull_report.get('sys_award_amount', 0)
    bull_sys_profit = float(bull_sys_bet_amount - bull_sys_award_amount) / bull_sys_bet_amount
    bull_gain_count = bull_report.get('gain_count', 0)
    bull_gain_amount = bull_report.get('gain_amount', 0)
    bull_lose_count = bull_report.get('lose_count', 0)
    bull_lose_amount = bull_report.get('lose_amount', 0)
    if bull_sys_profit < 0:
        msg = u'百人牛牛数据异常, 利润率{}\n赢家人数{},盈利{}\n输家人数{},盈利{}'.format(
            bull_sys_profit, bull_gain_count, bull_gain_amount,
            bull_lose_count, bull_lose_amount)
        notify(msg)


if __name__ == "__main__":
    start()
